package com.qf.utils;

import com.qf.bean.Employee;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * 22/11/2019
 * Echo
 * qmxc
 * Alibaba
 */
public class ExcelUtils {
    static HSSFWorkbook workbook;
    static HSSFSheet sheet;

    public static void createFirst(String headname) {
        workbook = new HSSFWorkbook();
        sheet = workbook.createSheet(headname);
        HSSFRow row = sheet.createRow(0);
        String[]heads={"账号","姓名","所属部门","角色"};
        for (int i=0;i<heads.length;i++){
            HSSFCell cell=row.createCell(i);
            cell.setCellValue(heads[i]);
        }
    }
    public static void createOthers(List<Employee>list){
        for(int i=0;i<list.size();i++){
            HSSFRow row = sheet.createRow(i+1);
            Employee employee = list.get(i);
            HSSFCell cella = row.createCell(0);
            cella.setCellValue(employee.getEmployeenum());
            HSSFCell cellb = row.createCell(1);
            cellb.setCellValue(employee.getUsername());
            HSSFCell cellc = row.createCell(2);
            cellc.setCellValue(employee.getDept().getDeptname());
            HSSFCell celld = row.createCell(3);
            celld.setCellValue(employee.getRole().getRname());
        }
    }

    public static void export(OutputStream outputStream)throws IOException{
          sheet.setGridsPrinted(true);
          workbook.write(outputStream);
    }



}
